/*
 * Created on 23.2.2007 by Mathew Stafurik
 */
package sk.sigp.eParser;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * class is responsible for handling with functions data and evaluating function results
 * parsing string to tokens for functions too ;)
 *   @author Mathew Stafurik
 *   @version 1.0
 */
class FunctionHolder {

    @SuppressWarnings("unchecked")
	private List _tokenizedElements=null;
    private VariableHolder _varHolder;
        
    /**
     * method responsible to parse string to desired token list
     * first token is name of function, others are parameters
     */
    @SuppressWarnings("unchecked")
	protected void tokenizeInput(String aStr) {
        getTokenizedElements().clear(); String wordGetter="";
        int xi=aStr.indexOf('('); 
        String str=aStr.substring(xi+1,aStr.length()-1);
        getTokenizedElements().add(aStr.subSequence(0,xi)); //name
        boolean skip=false; int inbound=0;
        
        for (int i=0;i<str.length();i++) {
            char c=str.charAt(i);
            if (c=='('&&!skip) { 
                skip=true; inbound=0;                
            }
            if (skip) {
                if (c=='(') inbound++;
                if (c==')') {
                    inbound--;
                    if (inbound==0) skip=false;
                }
                wordGetter+=c;
            } else {
                if (c==',') {
                    getTokenizedElements().add(wordGetter);
                    wordGetter="";
                } else wordGetter+=c;
            }
        }
        
        if (wordGetter.length()>0) getTokenizedElements().add(wordGetter);
    }        
    
    /**
     * just recalling static method from PairEvaluator
     * index is object index from tokenizedData
     */
    protected Double objectToValue(int index) {
        return PairEvaluator.convertElementToValue(getTokenizedElements().get(index),this,getVarHolder());
    }
    
    /**
     * method is responsible for parsing function data and returning function result
     */
    public Double executeFunction(String str) {
        tokenizeInput(str);
        String name=(String) getTokenizedElements().get(0);
        if (name.equals("if")) {
            if (PairEvaluator.doubleToBool(objectToValue(1)))
                return objectToValue(2); else return objectToValue(3);
        }
        if (name.equals("zaokruhli")) {
            Double val=objectToValue(1);
            Double roundScale=objectToValue(2);
            BigDecimal b=new BigDecimal(val.doubleValue());
            return new Double(b.setScale(roundScale.intValue(),BigDecimal.ROUND_HALF_UP).doubleValue());
        }
        return null;        
    }           
    
    @SuppressWarnings("unchecked")
	protected List getTokenizedElements() {
        if (_tokenizedElements==null) {
            _tokenizedElements=new ArrayList();
        }
        return _tokenizedElements;
    }

    protected VariableHolder getVarHolder() {
        return _varHolder;
    }

    public void setVarHolder(VariableHolder aVarHolder) {
        _varHolder = aVarHolder;
    }
}
